package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.dao.GoodsDao;
import com.woniuxy.dto.Page;
import com.woniuxy.entity.Goods;
import com.woniuxy.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: ideaAdmin
 * TODO:
 * Date: 2022/2/9:19:58
 * Description:
 */
@Service("goodsService")
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsDao goodsDao;

    //根据传进来的页码 返回商品数据
    @Override
    public Page<List<Goods>> findByPage(int pageNum, int pageSize, String categoryId, String bookname) {
        PageHelper.startPage(pageNum, pageSize);
        List<Goods> goodsList = goodsDao.findGoodsAll(categoryId, bookname);
        //获取分页信息
        PageInfo<Goods> pageInfo = new PageInfo<>(goodsList);
        System.out.println(pageInfo);
        //封装数据
        Page<List<Goods>> page = new Page<>();

        page.setTotalPage(pageInfo.getPages());//总页数
        page.setTotal(pageInfo.getTotal());//总条数
        page.setPageSize(pageSize);//页大小
        page.setCurrentPage(pageNum);//当前页
        page.setData(goodsList);//数据

        return page;
    }


    //根据id查找商品信息 返回数据
    @Override
    public Goods findById(int id) {
        return goodsDao.findByid(id);
    }

}
